#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstring>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

int main()
{
    int mode = 0666;
    int n = mkfifo("./my_namepipe", mode);
    if (n == -1)
    {
        perror("mkfifo");
        return -1;
    }
    
    cout<<"阻塞在open前"<<endl;
    int fd = open("my_namepipe", O_RDONLY);
    if (fd == -1)
    {
        perror("open");
        return -1;
    }
    cout<<"阻塞在open后"<<endl;
    char buff[1024];
    while (true)
    {
        int n = read(fd, buff, sizeof(buff) - 1);
        if (n > 0)
        {
            buff[n] = 0;
            cout << "服务器接收到的数据：" << buff << endl;
        }
    }

    close(fd);
    if(unlink("my_namepipe")==-1)
    {
        perror("unlink");
        return -1;
    }

    return 0;
}